package com.example.demo.controller.vuetest;

import com.example.demo.constant.VueStaticData;
import com.example.demo.entity.ClassesContentEntity;
import com.example.demo.entity.ClassesEntity;
import com.example.demo.entity.NormalListEntity;
import com.example.demo.entity.VueEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 高俊
 * @create 2021-12-2021/12/16-14:31
 */
@RestController
@RequestMapping("/vue")
@CrossOrigin
public class VueController {

    @RequestMapping("/select")
    public Object selectAll(){
        return VueStaticData.vueList;
    }

    @RequestMapping("/select/byId")
    public Object selectAllById(@RequestParam(value = "id",required = false) Long id){
        System.out.println(id);

        List<VueEntity> result = new ArrayList<>();
        if (id != null){
            for (VueEntity n : VueStaticData.vueList) {
                if (id.equals(n.getId())){
                    result.add(n);
                }
            }
            return result;
        }else {
            return null;
        }
    }

    @RequestMapping("/select/classes")
    public Object selectClassesAll(){
        return VueStaticData.clasList;
    }

    @RequestMapping("/select/content/by/id")
    public Object selectContentAll(@RequestParam("id") Long id){
        List<ClassesContentEntity> list = new ArrayList<>(10);
        for (ClassesContentEntity n : VueStaticData.contentList) {
            if (n.getGroupId().equals(id)){
                list.add(n);
            }
        }
        return list;
    }


    @RequestMapping("/select/normal")
    public Object selectNormalAll(){
        NormalListEntity[] normalList = VueStaticData.normalList;
        List<NormalListEntity> list = Arrays.asList(normalList);
        Map<Long, List<NormalListEntity>> collect = list.stream().collect(Collectors.groupingBy(NormalListEntity::getId));
        System.out.println(collect);
        return collect;
    }

    @RequestMapping("/select/recommed")
    public Object selectRecommedAll(){
        ClassesContentEntity[] list = VueStaticData.contentList;
        return list;
    }

    @RequestMapping("/add/car")
    public Boolean addCar(@RequestBody ClassesContentEntity entity){
        List<ClassesContentEntity> list = VueStaticData.carList;

        for (ClassesContentEntity n : list) {
            if (n.getId().equals(entity.getId())){
                n.setCount(n.getCount()+1);
                return true;
            }
        }
        list.add(entity);
        return true;
    }

    @RequestMapping("/select/car")
    public Object selectCarAll(){
        return VueStaticData.carList;
    }

    @RequestMapping("/update/car/commdity")
    public void updateCarCommdity(@RequestBody ClassesContentEntity entity){
        List<ClassesContentEntity> list = VueStaticData.carList;
        list.stream().forEach(n->{
            if (n.getId().equals(entity.getId())){
                n.setCount(entity.getCount());
            }
        });

    }

    @RequestMapping("/remove/car")
    public List<ClassesContentEntity> removeCar(@RequestParam("id") Long id){
        ClassesContentEntity entity = null;
        List<ClassesContentEntity> list = VueStaticData.carList;
        list = list.stream().filter(n -> !n.getId().equals(id)).collect(Collectors.toList());
        VueStaticData.carList = list;
        return list;
    }
}
